//
//  FSHomeViewController.m
//  WZSOA
//
//  Created by 刘沿 on 16/6/14.
//  Copyright © 2016年 刘沿. All rights reserved.
//
#import "ViewController3.h"

#import "FSHomeViewController.h"
#import "GDataDefines.h"
#import "FSFistViewController.h"
#import "FSSecondViewController.h"
#import "FSThirdViewController.h"

@interface FSHomeViewController ()

@property (weak, nonatomic) IBOutlet UIView *FirstView;


@property (weak, nonatomic) IBOutlet UIView *SecondView;


@property (weak, nonatomic) IBOutlet UIView *ThirdView;

@property (weak, nonatomic) IBOutlet UILabel *thirdLabel;

@property (weak, nonatomic) IBOutlet UILabel *secondLabel;
@property (weak, nonatomic) IBOutlet UILabel *firstLabel;

//姓名
@property (weak, nonatomic) IBOutlet UILabel *NameLabel;

//时间
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

//在办label
@property (weak, nonatomic) IBOutlet UILabel *ToDoingLabel;

//待办label
@property (weak, nonatomic) IBOutlet UILabel *upDoingLabel;



@end

@implementation FSHomeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self upApp];
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY年MM月dd日 "];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    
    self.timeLabel.text = dateString;
    
    FSFistViewController *firstVC = [[FSFistViewController alloc] init];
    
    if (firstVC.dataArray1) {
        
        self.firstLabel.hidden = YES;
        
        firstVC.view.frame = self.FirstView.bounds;
        
        [self.FirstView addSubview:firstVC.view];
        
    }else {
    
        self.firstLabel.hidden = NO;
    
    }
    
    
    FSSecondViewController *secVC = [[FSSecondViewController alloc] init];
    
    if (secVC.dataArray2) {
        
        self.secondLabel.hidden = YES;
        
        secVC.view.frame = self.SecondView.bounds;
        
        [self.SecondView addSubview:secVC.view];

    }else {
    
        self.secondLabel.hidden = NO;
        
    }
    
    
    FSThirdViewController *thirdVC = [[FSThirdViewController alloc] init];
   
    if (thirdVC.dataArray) {
        
        self.thirdLabel.hidden = YES;
        
        thirdVC.view.frame = self.ThirdView.bounds;
        
        [self.ThirdView addSubview:thirdVC.view];
    }else {
    
        self.thirdLabel.hidden = NO;
        
    }
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    [firstVC setFirstBlock:^(NSInteger index,NSString *field) {
        
        FSNewSendDetailController *sendD = [sb instantiateViewControllerWithIdentifier:@"NewSendDetailVC"];
        
        sendD.fileId = field;
        
        
//        [self presentViewController:sendD animated:YES completion:nil];
        
        [self.navigationController pushViewController:sendD animated:YES];
        
        
    }];
    
    [secVC setSecondBlock:^(NSInteger index, NSString *field) {
        
        FSNewReceiveDetailController *rec = [sb instantiateViewControllerWithIdentifier:@"NewDetailReceiveVC"];
        
        rec.fileId = field;
        
        [self.navigationController pushViewController:rec animated:YES];
        
    }];
    
    [thirdVC setEmailBlock:^(NSString *field) {
        
        FSEmailReceiveDetailController *emailDetail = [sb instantiateViewControllerWithIdentifier:@"EmailReceiveDetailVC"];
        
        emailDetail.fileId = field;
        
        [self.navigationController pushViewController:emailDetail animated:YES];
        
    }];
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"返回"
                                   style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:backButton];
    
    [self loadData];
    
}
- (void)upApp{
 //获取app版本号
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    
    NSArray *array = @[@{@"veseion":appVersion}];

[HttpTools requestWithURL:@"exchmain"
 arrays:array method:@"updateDown" success:^(id JSON) {
         NSArray *tempArray  = JSON[@"data"];
        NSDictionary * dict = [tempArray firstObject];
            NSString *url = dict[@"downlink"];
        NSString *index = dict[@"result"];
    
    
    if ([index isEqualToString:@"1"]) {
            
// 提示更新
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新" message:@"程序已经更新，请卸载完以前的版本，再重新下载" preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel  handler:^(UIAlertAction * _Nonnull action) {
                
//                  [_helper logoutVpn];

    FSLoginViewController * loginVC =  [self.storyboard instantiateViewControllerWithIdentifier:@"LoginVC"];

    self.view.window.rootViewController = loginVC;
                
            }];

            UIAlertAction *action = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                if (url.length != 0) {
                    
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

                }
                
            }];
            
            [alert addAction:cancelAction];
            [alert addAction:action];
            
            [self presentViewController:alert animated:YES completion:nil];
            
            
            
            
        }

    }failure:^(NSError *error) {
        
    }];

    

}


- (void)loadData{

    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];

    NSArray *array = @[@{@"userId":userId}];
    
    [HttpTools requestWithURL:Kexchmain arrays:array method:KIpadHome success:^(id JSON) {
        
        self.NameLabel.text = JSON[@"username"];
        
        self.ToDoingLabel.text = JSON[@"ydzbgz"];
        
        self.upDoingLabel.text = JSON[@"yddbgz"];
        
    } failure:^(NSError *error) {
        
    }];


}

//退出
- (IBAction)LogOut:(id)sender {
    

      UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:@"是否退出登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

if (buttonIndex == 1){
// [_helper logoutVpn];
     FSLoginViewController *loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewVC"];
        self.view.window.rootViewController = loginVC;


}
}


@end





















